package com.yxr.affairs;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yxr.affairs.api.ServerApi;
import com.yxr.affairs.api.model.ResultEntity;
import com.yxr.affairs.api.model.child.Login;
import com.yxr.affairs.base.BaseSwipeBackActivity;
import com.yxr.affairs.utils.Utils;

import butterknife.BindView;
import butterknife.OnClick;

public class StoreWVActivity extends BaseSwipeBackActivity {

    private static final String URL = environment > 0 ?
            "http://mlife.haochedai.com" : "http://mtlife.haochedai.com";
    //1
    //积分抽奖
    private static final String URL_CJ = URL + "/WapLottery/index.html";
    //2
    //等级规则
    private static final String URL_GZ = URL + "/WapMemberLevel/index.html";
    //3
    //物流查询
    private static final String URL_WL = URL + "/WapOrder/queryExpress.html";

    @BindView(R.id.sw_webview)
    WebView mWebView;

    @BindView(R.id.sw_title)
    TextView titleTv;

    @BindView(R.id.sw_gz)
    TextView gzTv;

    private String url;

    private boolean isRefresh = false;

    /**
     *
     * @param activity
     * @param flag 积分抽奖>1；等级规则>2；物流查询>3
     * @param order_id 没有传null
     */
    public static void startActivity(Activity activity, String flag, String order_id){
        Intent intent = new Intent(activity.getApplicationContext(), StoreWVActivity.class);
        if (flag != null){
            intent.putExtra("flag", flag);
        }
        if (order_id != null){
            intent.putExtra("order_id", order_id);
        }
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.store_webview;
    }

    @Override
    protected void initViews() {
        String flag = getIntent().getStringExtra("flag");
        if (flag.equals("1")) {
            url = ServerApi.redirectURL(URL_CJ);
            titleTv.setText("积分抽奖");
            gzTv.setVisibility(View.VISIBLE);

        }else if (flag.equals("2")) {
            url = ServerApi.redirectURL(URL_GZ);
            titleTv.setText("等级规则");

        }else if (flag.equals("3")) {
            String order_id = getIntent().getStringExtra("order_id");
            url = ServerApi.redirectURL(URL_WL + "?order_id=" + order_id);
            titleTv.setText("物流详情");

        }

        WebSettings webSettings = mWebView.getSettings();
        //设置可以支持缩放
        webSettings.setSupportZoom(true);
        //设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setSavePassword(false);

        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);

        webSettings.setSupportZoom(false);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);

        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.setWebChromeClient(new MyChromeClient());

        mWebView.loadUrl(url);

        if (mWebView != null) {
            mWebView.getViewTreeObserver().addOnScrollChangedListener(
                    new ViewTreeObserver.OnScrollChangedListener() {
                        @Override
                        public void onScrollChanged() {
                            if (mSwipeRefresh != null) {
                                mSwipeRefresh.setEnabled(mWebView.getScrollY() == 0);
                            }
                        }
                    });
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        this.isRefresh = isRefresh;
        mWebView.reload();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            _exit();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        return;
    }

    private void _exit() {
        if (mWebView.canGoBack()){
            mWebView.goBack();
            return;
        }
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.hold, R.anim.slide_right_exit);
    }

    @OnClick({R.id.sw_back_ib, R.id.sw_gz})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.sw_back_ib:
                _exit();
                break;
            case R.id.sw_gz:
                mWebView.loadUrl("javascript:pop1()");
                break;
            default:
                break;
        }
    }



    @Override
    protected void onError_(Throwable e) {

    }

    @Override
    protected void onCancel_() {

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
        }
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("http:") || url.startsWith("https:") ) {
                view.loadUrl(url);
                return false;
            }else{
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!isRefresh) {
                showLoading();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!Utils.isNetworkConnected(mContext)) {
                showNetError();
            }else {
                hideLoading();
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
        }
    }

    private class MyChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
//			if (!TextUtils.isEmpty(title)) {
//				titleTv.setText(title);
//			}
        }
    }

}
